package com.yida.gwj.common.service.impl;

import cn.hutool.core.bean.BeanUtil;
import cn.hutool.core.util.IdcardUtil;
import cn.hutool.core.util.PhoneUtil;
import com.yida.common.core.exception.ServiceException;
import com.yida.common.core.utils.StringUtils;
import com.yida.common.core.utils.poi.ExcelUtil;
import com.yida.gwj.common.domain.GwjBaseHouse;
import com.yida.gwj.common.domain.GwjBaseOwner;
import com.yida.gwj.common.domain.dto.GwjBaseOwnerHouseListDTO;
import com.yida.gwj.common.domain.request.GwjBaseOwnerUpdateRequest;
import com.yida.gwj.common.domain.vo.GwjBaseOwnerExcelVO;
import com.yida.gwj.common.mapper.GwjBaseHouseMapper;
import com.yida.gwj.common.mapper.GwjBaseOwnerMapper;
import com.yida.gwj.common.service.IGwjBaseOwnerService;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;
import org.springframework.web.multipart.MultipartFile;

import java.util.List;

/**
 * 基础数据（业主）Service业务层处理
 *
 * @author lqq
 * @date 2022-11-14
 */
@Service
public class GwjBaseOwnerServiceImpl implements IGwjBaseOwnerService {

	@Autowired
	private GwjBaseOwnerMapper ownerMapper;

	@Autowired
	private GwjBaseHouseMapper houseMapper;

	/**
	 * 获取业务业主集合
	 *
	 * @param houseCodes 房屋标识码集合
	 * @param areaId     地区ID
	 * @return /
	 */
	@Override
	public List<GwjBaseOwner> selectGwjBaseListByGwjBaseHouseCodes(String[] houseCodes, Long areaId) {
		return ownerMapper.selectGwjBaseListByGwjBaseHouseCodes(houseCodes, areaId);
	}

	/**
	 * 查询基础数据（业主）
	 *
	 * @param id 基础数据（业主）主键
	 * @return 基础数据（业主）
	 */
	@Override
	public GwjBaseOwner selectGwjBaseOwnerByGwjBaseOwnerId(Long id) {
		return ownerMapper.selectGwjBaseOwnerByGwjBaseOwnerId(id);
	}

	/**
	 * 查询基础数据（业主）
	 *
	 * @param houseCode 房屋标识码
	 * @param areaId    地区ID
	 */
	@Override
	public GwjBaseOwner selectGwjBaseOwnerByAreaIdAndHouseCode(String houseCode, Long areaId) {
		return ownerMapper.selectGwjBaseByGwjBaseHouseCodeAndAreaId(houseCode,areaId);
	}

	/**
	 * 新增基础数据（业主）
	 *
	 * @param owner 基础数据（业主）
	 * @return 结果
	 */
	@Override
	public int insertGwjBaseOwner(GwjBaseOwner owner) {
		return ownerMapper.insertGwjBaseOwner(owner);
	}

	/**
	 * 批量新增基础数据（业主）
	 *
	 * @param owners 基础数据（业主）
	 * @return 结果
	 */
	@Override
	public int insertBatchGwjBaseOwner(List<GwjBaseOwner> owners) {
		return ownerMapper.insertBatchGwjBaseOwner(owners);
	}

	/**
	 * 修改基础数据（业主）
	 *
	 * @param owner 基础数据（业主）
	 * @return 结果
	 */
	@Override
	public int updateGwjBaseOwner(GwjBaseOwner owner) {
		return ownerMapper.updateGwjBaseOwner(owner);
	}

	/**
	 * 修改基础数据（业主）
	 *
	 * @param gwjBaseOwners 基础数据（业主）
	 * @return 结果
	 */
	@Override
	public int updateBatchGwjBaseOwner(List<GwjBaseOwner> gwjBaseOwners) {
		return ownerMapper.updateBatchGwjBaseOwner(gwjBaseOwners);
	}

	/**
	 * 批量删除基础数据（业主）
	 *
	 * @param ids 需要删除的基础数据（业主）主键
	 * @return 结果
	 */
	@Override
	public int deleteGwjBaseOwnerByGwjBaseOwnerIds(Long[] ids) {
		return ownerMapper.deleteGwjBaseOwnerByGwjBaseOwnerIds(ids);
	}

	/**
	 * 删除基础数据（业主）信息
	 *
	 * @param id 基础数据（业主）主键
	 * @return 结果
	 */
	@Override
	public int deleteGwjBaseOwnerByGwjBaseOwnerId(Long id) {
		return ownerMapper.deleteGwjBaseOwnerByGwjBaseOwnerId(id);
	}

	/*************************新功能保留区************************************/

	@Override
	public List<GwjBaseOwner> selectGwjBaseOwnerList(GwjBaseOwner owner) {
		return ownerMapper.selectGwjBaseOwnerList(owner);
	}

	@Override
	public int insertImportGwjBaseOwner(List<GwjBaseOwner> owners) {
		return ownerMapper.insertImportGwjBaseOwner(owners);
	}

	@Override
	public int updateImportGwjBaseOwner(List<GwjBaseOwner> owners) {
		return ownerMapper.updateImportGwjBaseOwner(owners);
	}

	@Override
	public int deleteGwjBaseOwnerByHouseCodes(String[] houseCodes) {
		return ownerMapper.deleteGwjBaseOwnerByHouseCodes(houseCodes);
	}


	/*************************新功能保留区************************************/

	/**
	 * 修改基础数据（业主）
	 *
	 * @param request 基础数据（业主）
	 * @return 结果
	 */
	@Override
	public int updateGwjBaseOwnerByHouseId(GwjBaseOwnerUpdateRequest request) {
		if (StringUtils.isAllBlank(request.getCardNum(), request.getOwnerName(), request.getOwnerPhone())) {
			throw new ServiceException("请输入业主信息");
		}
		//查询房屋下是否有业主
		GwjBaseHouse gwjBaseHouse = houseMapper.selectGwjBaseHouseByGwjBaseHouseId(request.getHouseId());
		if (StringUtils.isNull(gwjBaseHouse)) {
			throw new ServiceException("查无房屋信息");
		}
		if (StringUtils.isNotBlank(request.getOwnerPhone()) && !PhoneUtil.isPhone(request.getOwnerPhone())) {
			throw new ServiceException("请输入正确手机号码且只能输入一个");
		}
		if (StringUtils.isNotBlank(request.getCardNum()) && !IdcardUtil.isValidCard(request.getCardNum())) {
			throw new ServiceException("请输入正确身份证号码");
		}
		//查询业主
		GwjBaseOwner gwjBaseOwner = ownerMapper.selectGwjBaseByGwjBaseHouseCodeAndAreaId(gwjBaseHouse.getHouseCode(), gwjBaseHouse.getAreaId());
		GwjBaseOwner newGwjBaseOwner = BeanUtil.copyProperties(request, GwjBaseOwner.class);
		newGwjBaseOwner.setAreaId(gwjBaseHouse.getAreaId());
		newGwjBaseOwner.setStreetId(gwjBaseHouse.getStreetId());
		newGwjBaseOwner.setHouseCode(gwjBaseHouse.getHouseCode());
		if (StringUtils.isNull(gwjBaseOwner)) {
			//做新增
			return insertGwjBaseOwner(newGwjBaseOwner);
		} else {
			//修改
			newGwjBaseOwner.setOwnerId(gwjBaseOwner.getOwnerId());
			return updateGwjBaseOwner(newGwjBaseOwner);
		}
	}

	/**
	 * 导入投票业主信息
	 *
	 * @param file        业主文件
	 * @param communityId 小区ID
	 * @return /
	 * @throws Exception /
	 */
	@Override
	public String importGwjBaseOwner(MultipartFile file, Long communityId) throws Exception {
		ExcelUtil<GwjBaseOwnerExcelVO> util = new ExcelUtil<>(GwjBaseOwnerExcelVO.class);
		List<GwjBaseOwnerExcelVO> gwjBaseOwnerExcelVos = util.importExcel(file.getInputStream());
		if (StringUtils.isEmpty(gwjBaseOwnerExcelVos)) {
			throw new ServiceException("导入失败,导入业主不能为空！");
		}
		int successNum = 0;
		for (GwjBaseOwnerExcelVO vo : gwjBaseOwnerExcelVos) {
			if (StringUtils.isNull(vo.getHouseId())) {
				continue;
			}
			//查询房屋是否存在
			GwjBaseHouse gwjBaseHouse = houseMapper.selectGwjBaseHouseByGwjBaseHouseId(vo.getHouseId());
			if (StringUtils.isNull(gwjBaseHouse)) {
				continue;
			}
			//查询房屋下是否有业主
			GwjBaseOwner gwjBaseOwner = ownerMapper.selectGwjBaseByGwjBaseHouseCodeAndAreaId(gwjBaseHouse.getHouseCode(), gwjBaseHouse.getAreaId());
			GwjBaseOwner newGwjBaseOwner = BeanUtil.copyProperties(vo, GwjBaseOwner.class);
			newGwjBaseOwner.setAreaId(gwjBaseHouse.getAreaId());
			newGwjBaseOwner.setStreetId(gwjBaseHouse.getStreetId());
			newGwjBaseOwner.setHouseCode(gwjBaseHouse.getHouseCode());
			//校验格式
			if (!PhoneUtil.isPhone(newGwjBaseOwner.getOwnerPhone())) {
				throw new ServiceException("请输入正确手机号码且只能输入一个");
			}
			if (!IdcardUtil.isValidCard(newGwjBaseOwner.getCardNum())) {
				throw new ServiceException("请输入正确身份证号码");
			}
			if (StringUtils.isNull(gwjBaseOwner)) {
				//做新增
				if (1 != insertGwjBaseOwner(newGwjBaseOwner)) {
					throw new ServiceException("导入失败,新增业主数据失败");
				}
			} else {
				//修改
				newGwjBaseOwner.setOwnerId(gwjBaseOwner.getOwnerId());
				if (1 != updateGwjBaseOwner(newGwjBaseOwner)) {
					throw new ServiceException("导入失败,更新业主数据失败");
				}
			}
			successNum++;
		}
		return "恭喜您，成功导入" + successNum + " 条数据";
	}

	@Override
	public List<GwjBaseOwnerHouseListDTO> selectGwjOwnerHouseList(GwjBaseOwner gwjBaseOwner) {
		return ownerMapper.selectGwjOwnerHouseList(gwjBaseOwner);
	}
}

